package com.czy.owner.ui.workorder.orderviewholder;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.MyOrderModel;
import com.czy.owner.entity.WorkOrderModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.workorder.OrderFragment;
import com.czy.owner.ui.workorder.PaidSubscriptionActivity;
import com.czy.owner.ui.workorder.SubscribeOrdelActivity;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.CountdownView;
import com.czy.owner.widget.UniversalDialog;
import com.easyrecycleview.adapter.BaseViewHolder;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created hep on 2017/8/28.
 */
public class OrderCancelledViewHolder2 extends BaseViewHolder<WorkOrderModel> {

    private TextView tv_order_number, tv_order_state, tv_store_name, tv_order_item, tv_order_service_time, tv_order_jishi, tv_order_dingjing, tv_order_num;
    private LinearLayout linear_order_num;
    private Context ctx;

    /**
     * @param parent
     * @param ctx
     */
    public OrderCancelledViewHolder2(ViewGroup parent, Context ctx) {
        super(parent, R.layout.item_order_cancle2);
        tv_order_number = $(R.id.tv_order_number);
        tv_order_item = $(R.id.tv_order_item);
        tv_order_service_time = $(R.id.tv_order_service_time);
        tv_order_state = $(R.id.tv_order_state);
        tv_order_jishi = $(R.id.tv_order_jishi);
        tv_order_dingjing = $(R.id.tv_order_dingjing);
        tv_order_num = $(R.id.tv_order_num);
        linear_order_num = $(R.id.linear_order_num);
        tv_store_name = $(R.id.tv_store_name);

        this.ctx = ctx;
    }

    @Override
    public void setData(final WorkOrderModel model) {
        tv_store_name.setText(model.getStoreName());
        if (model.getStoreBookingNumber() != null && !model.getStoreBookingNumber().isEmpty()) {
            if (linear_order_num.getVisibility() == View.GONE)
                linear_order_num.setVisibility(View.VISIBLE);
            tv_order_num.setText(model.getStoreBookingNumber());
        } else {
            if (linear_order_num.getVisibility() == View.VISIBLE)
                linear_order_num.setVisibility(View.GONE);
        }
//        String daytime = TimeUtils.timeStamp2Date(model.getServiceTime(), "HH");
//        String huor;
//        if (Integer.parseInt(daytime) > 12) {
//            huor = "上午";
//        } else {
//            huor = "下午";
//        }
//        tv_order_service_time.setText(TimeUtils.timeStamp2Date(model.getServiceTime(), "yyyy-MM-dd") + " " + TimeUtils.getWeeky(Long.parseLong(model.getServiceTime()))
//                + huor);
        tv_order_service_time.setText(model.getConvertServiceTime());
        tv_order_number.setText("工单编号: " + model.getOrderNumber());
//        tv_order_service_time.setText(TimeUtils.timeStamp2Date(model.getServiceTime(), "yyyy-MM-dd HH:mm"));

        tv_order_item.setText(StringUtils.getString(model.getOrderItemNameStr(), "无"));
        tv_order_jishi.setText(model.getLicenseNumber() + "  " + model.getChName());
//        tv_order_dingjing.setText("¥"+DecimalUtil.formatNum(StringUtils.getString(model.getDepositRefund(), "0")));
        tv_order_dingjing.setText("¥" + DecimalUtil.formatNum(model.getTotalPrice()));

    }

    private void DeleteOrderDialog(final WorkOrderModel model, String title, String sure, String cancle) {
        new UniversalDialog(ctx).builder().setTitle(title)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteOrder(model);
                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    private void deleteOrder(final WorkOrderModel model) {
        String url = Constants.APP_API_BASE + Constants.OWN_ORDER_ORDERDELORDE;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(ctx).getSession());
        params.addBodyParameter("storeId", model.getStoreId() + "");
        params.addBodyParameter("orderNumber", model.getOrderNumber());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess==" + result);
                String flag = JsonUtil.getJsonValuesString(result, "flag");
                String exp = JsonUtil.getJsonValuesString(result, "exp");
                if (flag.equals("true")) {
                    //EventBus.getDefault().post(new UpdateOrderMessageEvent("订单统计"));
                    Intent intent = new Intent();
                    intent.setAction(SubscribeOrdelActivity.orderAction);
                    if (SubscribeOrdelActivity.currentId() == 0) {
                        intent.putExtra("currentId", 0);
                    } else {
                        intent.putExtra("currentId", 4);
                    }
                    ctx.sendBroadcast(intent);
                } else {
                    PhoneUtils.ShowToastMessage(ctx, exp);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError==" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
